﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Extentions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using TrainingReminderSystem.Bll;
using TrainingReminderSystem.Model;

namespace TrainingReminderSystem.ApiControllers
{
    /// <summary>
    /// 
    /// </summary>
    //[Route("api/{controller}/{action}/{id?}")]
    [ApiController]
    [EnableCors("any")]
    [AllowAnonymous]
    public class PublicController : BaseControllerBase
    {
        #region 启动webSocket

        [HttpGet]
        [Route("api/Public/ChangeStart")]
        public AjaxResult<bool> ChangeStart()
        {
            WebSocketBll.WebSocketSuccess = false;
            return AjaxResult<bool>.SuccessResult(true);
        }

        [HttpGet]
        [Route("api/Public/Start")]
        public AjaxResult<bool> Start()
        {
            if (WebSocketBll.WebSocketSuccess == false)
            {
                WebSocketBll.Start();
            }
            return AjaxResult<bool>.SuccessResult(true);
        }

        #endregion

        ///// <summary>
        ///// Get1方法
        ///// </summary>
        ///// <param name="id">参数ID</param>
        ///// <returns></returns>
        //[Route("api/Public/Get/{id?}")]
        //[HttpGet]
        //[ApiExplorerSettings(IgnoreApi = true)]
        //public string Get(int? id)
        //{
        //    return id == null ? "空" : id.ToString();
        //}

        ///// <summary>
        ///// 新增/编辑方法
        ///// </summary>
        ///// <param name="model">form对象</param>
        ///// <returns></returns>
        //[Route("api/Public/TblTestAdd")]
        //[HttpPost]
        //public AjaxResult<dynamic> TblTestAdd([FromForm] TblTest model)
        //{
        //    var bll = new TestBll();
        //    var result = bll.TblTestSave(model);
        //    return result;
        //}

        //[Route("api/Public/TblTestGet")]
        //[HttpPost]
        //public AjaxResult<dynamic> TblTestGet([FromForm] int id, [FromForm] string name)
        //{
        //    var bll = new TestBll();
        //    var result = bll.TblTestGet(id);
        //    return result;
        //}

        ///// <summary>
        ///// get2方法
        ///// </summary>
        ///// <param name="form">form对象</param>
        ///// <returns></returns>
        //[Route("api/Public/TblTestList")]
        //[HttpPost]
        //public AjaxResult<dynamic> TblTestList([FromForm] IFormCollection form)
        //{
        //    var bll = new TestBll();
        //    var result = bll.GetListByPage(form["index"].ExToInt32(), form["index"].ExToInt32(), form);
        //    return result;
        //}

        /// <summary>
        /// 空json
        /// </summary>
        /// <param name="search">form对象</param>
        /// <returns></returns>
        [Route("api/Public/List")]
        [HttpPost]
        public AjaxResult<dynamic> List([FromForm] IFormCollection search)
        {
            string json = @"[{
    ""uuid"": ""6e4d4CEF-E3c6-d962-ccb4-FC21Ec3BE067"",
    ""id"": 1,
    ""title"": ""称。"",
    ""status"": ""published"",
    ""author"": ""马娜"",
    ""datetime"": ""2015-03-25 05:19:35"",
    ""pageViews"": 4129,
    ""img"": ""https://picsum.photos/200/200?random=c20Ff3dc-90C5-CBFC-831A-C7Fe1d9cf5aC"",
    ""smallImg"": ""https://picsum.photos/40/40?random=0B8c3Bcd-bCe8-9BD7-A1cC-909f217eCbc4"",
    ""switch"": true,
    ""percent"": 87}]";//System.IO.File.ReadAllText(Base64Helper.CurrentDirectory + "temp/article.json");
            string id = search["id"];
            string title = search["title"];
            string uuid = search["uuid"];
            string id__title = search["id__title"];
            string uuid__Title = search["uuid__Title"];
            var arr = JsonHelper.DeserializeObject(json) as JArray;
            List<JToken> result = arr.ToList();
            if (!string.IsNullOrEmpty(id))
            {
                result = result.Where(m => m["id"].ToString() == id).ToList();
            }
            if (!string.IsNullOrEmpty(title))
            {
                result = result.Where(m => m["title"].ToString().Contains(title)).ToList();
            }
            if (!string.IsNullOrEmpty(uuid))
            {
                result = result.Where(m => m["uuid"].ToString() == uuid).ToList();
            }
            if (!string.IsNullOrEmpty(id__title))
            {
                result = result.Where(m => m["id"].ToString() == id__title || m["title"].ToString().Contains(id__title)).ToList();
            }
            if (!string.IsNullOrEmpty(uuid__Title))
            {
                result = result.Where(m => m["uuid"].ToString() == uuid__Title || m["title"].ToString().Contains(uuid__Title)).ToList();
            }

            return AjaxResult<dynamic>.SuccessResult(result);
        }
    }
}
